//package com.fundodoo.sfe.admin.controller;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Collection;
//import java.util.List;
//import java.util.Locale;
//import java.util.Map;
//
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.poi.util.StringUtil;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.MessageSource;
//import org.springframework.context.i18n.LocaleContextHolder;
//import org.springframework.stereotype.Controller;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.fundodoo.sfe.common.annotation.Log;
//import com.fundodoo.sfe.common.controller.BaseController;
//import com.fundodoo.sfe.common.po.PermissionInfo;
//import com.fundodoo.sfe.domain.Dept;
//import com.tpv.vc.common.domain.ResponseBo;
//import com.tpv.vc.common.domain.Tree;
//import com.tpv.vc.system.service.DeptMenuService;
//import com.tpv.vc.system.service.DeptService;
//
//import io.undertow.util.FileUtils;
//
//@Controller
//public class DeptController extends BaseController{
//
//    private Logger log = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private DeptService deptService;
//    
//    @Autowired
//    private DeptMenuService deptMenuService;
//    
//    @Autowired
//    private MessageSource messageSource;
////    private Locale locale = LocaleContextHolder.getLocale();
//
//    @Log("访问组管理页面")
//    @RequestMapping("dept")
//    @RequiresPermissions("dept:list")
//    public String index() {
//        return "system/dept/dept";
//    }
//
//    @Log("获取用户组树菜单")
//    @RequestMapping("dept/tree")
//    @ResponseBody
//    public ResponseBo getDeptTree(Dept dept) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	dept.setParentId(null == permissionInfo.getDeptId() ? 0 : permissionInfo.getDeptId());
//        	dept.setSelf("1");
//            Tree<Dept> tree = this.deptService.getDeptTree(dept);
//            return ResponseBo.ok(tree);
//        } catch (Exception e) {
//            log.error("获取用户组树失败", e);
//            return ResponseBo.error(messageSource.getMessage("dept.get.tree.fail", null, locale));
//        }
//    }
//
//    @Log("获取用户组")
//    @RequestMapping("dept/getDept")
//    @ResponseBody
//    public ResponseBo getDept(Long deptId) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
////            Dept dept = this.deptService.findById(deptId);
//        	Map<String,Object> deptCascadingInfo = this.deptService.findDeptCascadingById(deptId);
//            return ResponseBo.ok(deptCascadingInfo);
//        } catch (Exception e) {
//            log.error("获取用户组信息失败", e);
//            return ResponseBo.error(messageSource.getMessage("dept.get.info.fail", null, locale));
//        }
//    }
//
//    @Log("获取用户组列表")
//    @RequestMapping("dept/list")
//    @RequiresPermissions("dept:list")
//    @ResponseBody
//    public List<Dept> deptList(Dept dept) {
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	dept.setParentId(null == permissionInfo.getDeptId() ? 0 : permissionInfo.getDeptId());
//        	dept.setSelf("0");
//            return this.deptService.findAllDepts(dept);
//        } catch (Exception e) {
//            log.error("获取用户组列表失败", e);
//            return new ArrayList<>();
//        }
//    }
//
//    @Log("导出用户组excel")
//    @RequestMapping("dept/excel")
//    @ResponseBody
//    public ResponseBo deptExcel(Dept dept) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            List<Dept> list = this.deptService.findAllDepts(dept);
//            return FileUtils.createExcelByPOIKit("用户组表", list, Dept.class);
//        } catch (Exception e) {
//            log.error("导出用户组信息Excel失败", e);
//            return ResponseBo.error(messageSource.getMessage("export.fail", null, locale));
//        }
//    }
//
//    @Log("导出用户组csv")
//    @RequestMapping("dept/csv")
//    @ResponseBody
//    public ResponseBo deptCsv(Dept dept) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            List<Dept> list = this.deptService.findAllDepts(dept);
//            return FileUtils.createCsv("用户组表", list, Dept.class);
//        } catch (Exception e) {
//            log.error("获取用户组信息Csv失败", e);
//            return ResponseBo.error(messageSource.getMessage("export.fail", null, locale));
//        }
//    }
//
//    @RequestMapping("dept/checkDeptName")
//    @ResponseBody
//    public boolean checkDeptName(String deptName, String oldDeptName) {
//        if (StringUtils.isNotBlank(oldDeptName) && deptName.equalsIgnoreCase(oldDeptName)) {
//            return true;
//        }
//        Dept result = this.deptService.findByName(deptName);
//        return result == null;
//    }
//
//    @Log("新增用户组 ")
//    @RequiresPermissions("dept:add")
//    @RequestMapping("dept/add")
//    @ResponseBody
//    public ResponseBo addRole(Dept dept, Long[] meetingRoomId, Long[] menuId) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	if(null == dept.getParentId()) {
//        		dept.setParentId(permissionInfo.getDeptId());
//        	}
//			//add by jm.hong 20190115 删除根节点(根节点只是用来展示)=====start=====
//        	meetingRoomId = StringUtil.deleteArrayData(meetingRoomId, 0l);//删除根节点
//        	menuId = StringUtil.deleteArrayData(menuId, 0l);//删除根节点
//		    //add by jm.hong 20190115 删除根节点(根节点只是用来展示)=====end===== 
//        	//modify by jm.hong at 20190122 子用户组菜单不能超过父用户组=====start=====
//        	List<Long> menuList=Arrays.asList(menuId);
//        	Collection<Long> disColl = getDisColl(dept,menuList);
//        	if(CollectionUtils.isNotEmpty(disColl)) {
//        		return ResponseBo.error(messageSource.getMessage("dept.children.menu.more.than.parent", null, locale));
//        	}
//        	//modify by jm.hong at 20190122 子用户组菜单不能超过父用户组=====end=====
//			this.deptService.addDept(dept,meetingRoomId,menuId,permissionInfo.getDeptId());
//            return ResponseBo.ok(messageSource.getMessage("dept.add.success", null, locale));
//        } catch (Exception e) {
//            log.error("新增用户组失败", e);
//            return ResponseBo.error(messageSource.getMessage("dept.add.fail", null, locale));
//        }
//    }
//
//    @Log("删除用户组")
//    @RequiresPermissions("dept:delete")
//    @RequestMapping("dept/delete")
//    @ResponseBody
//    public ResponseBo deleteDepts(String ids) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            this.deptService.deleteDepts(ids);
//            return ResponseBo.ok(messageSource.getMessage("dept.delete.success", null, locale));
//        } catch (Exception e) {
//            log.error("删除用户组失败", e);
//            return ResponseBo.error(messageSource.getMessage("dept.delete.fail", null, locale));
//        }
//    }
//
//    @Log("修改用户组 ")
//    @RequiresPermissions("dept:update")
//    @RequestMapping("dept/update")
//    @ResponseBody
//    public ResponseBo updateRole(Dept dept,Long[] meetingRoomId, Long[] menuId) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	if(null == dept.getParentId()) {
//        		dept.setParentId(permissionInfo.getDeptId());
//        	}
//        	//add by jm.hong 20190115 删除根节点(根节点只是用来展示)=====start=====
//        	meetingRoomId = StringUtil.deleteArrayData(meetingRoomId, 0l);//删除根节点
//        	menuId = StringUtil.deleteArrayData(menuId, 0l);//删除根节点
//        	//add by jm.hong 20190115 删除根节点(根节点只是用来展示)=====end=====   
//        	//modify by jm.hong at 20190122 子用户组菜单不能超过父用户组=====start=====
//        	List<Long> menuList=Arrays.asList(menuId);
//        	Collection<Long> disColl = getDisColl(dept,menuList);
//        	if(CollectionUtils.isNotEmpty(disColl)) {
//        		return ResponseBo.error(messageSource.getMessage("dept.children.menu.more.than.parent", null, locale));
//        	}
//        	//modify by jm.hong at 20190122 子用户组菜单不能超过父用户组=====end=====
//            this.deptService.updateDept(dept,meetingRoomId, menuId,permissionInfo.getDeptId());
//            String successMsg = messageSource.getMessage("dept.update.success", null, locale);
//            return ResponseBo.ok(successMsg);
//        } catch (Exception e) {
//            log.error("修改用户组失败", e);
//            return ResponseBo.error(messageSource.getMessage("dept.update.fail", null, locale));
//        }
//    }
//    
//    public Collection<Long> getDisColl (Dept dept,List<Long> menuList){
//    	Long parentId = dept.getParentId();
//    	List<Long> parentMenuIds = deptMenuService.findMenuIdsByDeptId(parentId);
//    	Collection<Long> interColl = CollectionUtils.intersection(menuList, parentMenuIds);//取交集
//    	Collection<Long> disColl = CollectionUtils.disjunction(menuList, interColl);//取差集
//    	return disColl;
//    	
//    }
//}
